package ee.greaton.kultuuriaken;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapViewActivity extends MapActivity{
	
	Intent intent;
	Event e;
	InputStream is;


	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.maplayout);
	    
	    intent = getIntent();
	    
	    e = (Event) intent.getSerializableExtra(EventViewActivity.SINGLE_EVENT_EXTRA);
	    
	    int gLat = (int) (Float.parseFloat(e.getGeoLat()) * 1000000);
	    int gLong = (int) (Float.parseFloat(e.getGeoLong()) * 1000000);
	    
	    MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    List<Overlay> mapOverlays = mapView.getOverlays();
	    Drawable drawable = this.getResources().getDrawable(R.drawable.myarrow);
	    MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);
	    
	    GeoPoint point = new GeoPoint(gLat, gLong);
	    OverlayItem overlayitem = new OverlayItem(point, e.getTitle(), e.getLocation() + "\n(" + ((float) gLat/1000000) + " , " + ((float) gLong/1000000) + ")");
	    
	    itemizedoverlay.addOverlay(overlayitem);
	    mapOverlays.add(itemizedoverlay);
	    
	    MapController mapController = mapView.getController();
	    
	    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO) {
        	mapController.animateTo(point, new Message());
        	mapController.setZoom(16);
        }
	}
	
	public static Address getGeoData(String address, Context context) {
		Geocoder gcd = new Geocoder(context, Locale.getDefault());;
		List<Address> location = null;
		Address data = null;
		try {
			location = gcd.getFromLocationName(address, 1);
			if(location.size() > 0){
				data = location.get(0);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return data;
	}

}
